package com.aims.server.controller;

import com.aims.common.model.R;
import com.aims.server.domain.dto.AccountWorkbenchQueryDTO;
import com.aims.server.domain.vo.*;
import com.aims.server.service.AccountWorkbenchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 账户办理工作台Controller
 */
@RestController
@RequestMapping("/api/account-workbench")
@CrossOrigin
public class AccountWorkbenchController {

    @Autowired
    private AccountWorkbenchService accountWorkbenchService;

    /**
     * 获取工作台数据（包含四泳道）
     *
     * @param query 查询条件
     * @return 工作台数据
     */
    @PostMapping("/workbench")
    public R<WorkbenchDataVO> getWorkbenchData(@RequestBody AccountWorkbenchQueryDTO query) {
        try {
            WorkbenchDataVO workbenchData = accountWorkbenchService.getWorkbenchData(query);
            return R.success(workbenchData);
        } catch (Exception e) {
            return R.error("获取工作台数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取日历计数
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 日期->任务数量映射
     */
    @GetMapping("/calendar-counts")
    public R<Map<String, Integer>> getCalendarCounts(
            @RequestParam String startDate,
            @RequestParam String endDate) {
        try {
            Map<String, Integer> counts = accountWorkbenchService.getCalendarCounts(startDate, endDate);
            return R.success(counts);
        } catch (Exception e) {
            return R.error("获取日历计数失败: " + e.getMessage());
        }
    }

    /**
     * 获取卡片详情
     *
     * @param cardId 卡片ID
     * @return 卡片详情
     */
    @GetMapping("/card/{cardId}")
    public R<AccountCardVO> getCardDetail(@PathVariable String cardId) {
        try {
            AccountCardVO card = accountWorkbenchService.getCardDetail(cardId);
            if (card == null) {
                return R.error("卡片不存在");
            }
            return R.success(card);
        } catch (Exception e) {
            return R.error("获取卡片详情失败: " + e.getMessage());
        }
    }

    /**
     * 获取日历视图数据
     *
     * @param query 查询条件
     * @return 日历视图数据
     */
    @PostMapping("/calendar")
    public R<AccountCalendarVO> getCalendarView(@RequestBody AccountWorkbenchQueryDTO query) {
        try {
            AccountCalendarVO calendarVO = accountWorkbenchService.getCalendarView(query);
            return R.success(calendarVO);
        } catch (Exception e) {
            return R.error("获取日历视图失败: " + e.getMessage());
        }
    }

    /**
     * 获取任务列表
     *
     * @param query 查询条件
     * @return 任务列表
     */
    @PostMapping("/tasks")
    public R<List<AccountTaskVO>> getTaskList(@RequestBody AccountWorkbenchQueryDTO query) {
        try {
            List<AccountTaskVO> taskList = accountWorkbenchService.getTaskList(query);
            return R.success(taskList);
        } catch (Exception e) {
            return R.error("获取任务列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取任务详情
     *
     * @param processInstanceId 流程实例ID
     * @return 任务详情
     */
    @GetMapping("/task/{processInstanceId}")
    public R<AccountTaskVO> getTaskDetail(@PathVariable Long processInstanceId) {
        try {
            AccountTaskVO taskVO = accountWorkbenchService.getTaskDetail(processInstanceId);
            if (taskVO == null) {
                return R.error("任务不存在");
            }
            return R.success(taskVO);
        } catch (Exception e) {
            return R.error("获取任务详情失败: " + e.getMessage());
        }
    }

    /**
     * 获取任务统计
     *
     * @param query 查询条件
     * @return 任务统计
     */
    @PostMapping("/statistics")
    public R<TaskStatisticsVO> getTaskStatistics(@RequestBody AccountWorkbenchQueryDTO query) {
        try {
            TaskStatisticsVO statistics = accountWorkbenchService.getTaskStatistics(query);
            return R.success(statistics);
        } catch (Exception e) {
            return R.error("获取任务统计失败: " + e.getMessage());
        }
    }

    /**
     * 更新任务进度
     *
     * @param processInstanceId 流程实例ID
     * @param progressCode 进度代码
     * @param remark 备注
     * @return 操作结果
     */
    @PutMapping("/task/{processInstanceId}/progress")
    public R<Void> updateTaskProgress(
            @PathVariable Long processInstanceId,
            @RequestParam String progressCode,
            @RequestParam(required = false) String remark) {
        try {
            accountWorkbenchService.updateTaskProgress(processInstanceId, progressCode, remark);
            return R.success();
        } catch (Exception e) {
            return R.error("更新任务进度失败: " + e.getMessage());
        }
    }

    /**
     * 标记任务为加急
     *
     * @param processInstanceId 流程实例ID
     * @param isUrgent 是否加急(1-是,2-否)
     * @return 操作结果
     */
    @PutMapping("/task/{processInstanceId}/urgent")
    public R<Void> markTaskUrgent(
            @PathVariable Long processInstanceId,
            @RequestParam Integer isUrgent) {
        try {
            accountWorkbenchService.markTaskUrgent(processInstanceId, isUrgent);
            return R.success();
        } catch (Exception e) {
            return R.error("标记加急失败: " + e.getMessage());
        }
    }
}